package com.tgpms.web.system.service.impl;

import com.tgpms.web.system.entity.HbSysMenu;
import com.tgpms.web.system.entity.HbUsersMenu;
import com.tgpms.web.system.dao.HbSysMenuMapper;
import com.tgpms.web.system.dao.HbUsersMenuMapper;
import com.tgpms.web.system.service.IHbUsersMenuService;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;

/**
 * <p>
 * 环保用户菜单关联表 服务实现类
 * </p>
 *
 * @author newway
 * @since 2020-09-14
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
@Slf4j
public class HbUsersMenuServiceImpl implements IHbUsersMenuService {

	@Resource
	private HbUsersMenuMapper hbUsersMenumapper;
	@Resource
	private HbSysMenuMapper hbSysMenuMapper;

	/**
	 * 查询用户菜单集合接口
	 */
	@Override
	public Result getHbUsersMenus(String username,String type, Result result) {
		List<Object> list = new ArrayList<Object>();
		//查询用户菜单集合
		QueryWrapper<HbUsersMenu> wrapper1 = new QueryWrapper<HbUsersMenu>();
		wrapper1.eq("USERS_ID", username);
		HbUsersMenu menus = hbUsersMenumapper.selectOne(wrapper1);
		if(menus!=null && menus.getMenuIds()!=null) {
			List<String> umenuList = new ArrayList<String>(Arrays.asList(menus.getMenuIds().split(",")));
			QueryWrapper<HbSysMenu> wrapper2 = new QueryWrapper<HbSysMenu>();
			if(!type.equals("default")) {
				wrapper2.eq("MENU_TYPE", type);
				wrapper2.ne("PARENT_ID", "0");
			}else {
				wrapper2.eq("PARENT_ID", "0");
			}
			wrapper2.in("ID", umenuList);
			wrapper2.orderByAsc("MENU_ORDER");

			List<HbSysMenu> menulist = hbSysMenuMapper.selectList(wrapper2);
			if(menulist!=null && menulist.size()>0) {
				for(HbSysMenu m : menulist) {
					JSONObject mjson = new JSONObject();
					mjson.put("id", m.getId());
					mjson.put("title", m.getMenuName());
					mjson.put("path", m.getMenuUrl());
					mjson.put("name", m.getMenuUrl());
					mjson.put("index", m.getMenuOrder());
					List<Object> l = getMenusChildren(m.getId(),umenuList);
					if(l!=null && l.size()>0) {
						mjson.put("subs", l);
					}
					list.add(mjson);
				}
			}
			result.setMsg("查询成功");
			result.setData(list);
			return result;
		}
		else {
			result.setSuccess(false);
			result.setData(list);
			result.setMsg("无菜单权限");
			return result;
		}
	}

	//用户菜单树形子集查询
	private List<Object> getMenusChildren(String parentId,List<String> umenuList) {
		List<Object> list = new ArrayList<Object>();
		QueryWrapper<HbSysMenu> wrapper1 = new QueryWrapper<HbSysMenu>();
		wrapper1.eq("PARENT_ID", parentId);
		wrapper1.in("ID", umenuList);
		wrapper1.orderByAsc("MENU_ORDER");
		List<HbSysMenu> menulist = hbSysMenuMapper.selectList(wrapper1);
		if(menulist!=null && menulist.size()>0) {
			for(HbSysMenu m : menulist) {
				JSONObject mjson = new JSONObject();
				mjson.put("id", m.getId());
				mjson.put("title", m.getMenuName());
				mjson.put("path", m.getMenuUrl());
				mjson.put("name", m.getMenuUrl());
				mjson.put("index", m.getMenuOrder());
				List<Object> l = getMenusChildren(m.getId(),umenuList);
				if(l!=null && l.size()>0) {
					mjson.put("subs", l);
				}
				list.add(mjson);
			}
		}
		return list;
	}

	/**
	 * 配置查询用户菜单权限树形接口
	 */
	@Override
	public Result getHbUsersMenuInfoForTree(String username,Result result) {
		List<Object> list = new ArrayList<Object>();
		JSONObject json = new JSONObject();
		//查询用户菜单集合
		QueryWrapper<HbUsersMenu> wrapper1 = new QueryWrapper<HbUsersMenu>();
		wrapper1.eq("USERS_ID", username);
		HbUsersMenu menus = hbUsersMenumapper.selectOne(wrapper1);
		String menustr = "";
		if(menus!=null && menus.getMenuIds()!=null) {
			json.put("menus", menus.getMenuIds());
			menustr = ","+json.getString("menus")+",";
		}
		QueryWrapper<HbSysMenu> wrapper = new QueryWrapper<HbSysMenu>();
		wrapper.eq("PARENT_ID", 0);
		wrapper.orderByAsc("MENU_ORDER");
		List<HbSysMenu> menulist = hbSysMenuMapper.selectList(wrapper);
		if(menulist!=null && menulist.size()>0) {
			for(HbSysMenu m : menulist) {
				if(json.get("menus")!=null) {
					menustr = menustr.replaceAll(","+m.getId()+",", ",");
				}
				JSONObject mjson = new JSONObject();
				mjson.put("id", m.getId());
				mjson.put("label", m.getMenuName());
				List<Object> l = getChildren(m.getId());
				if(l!=null && l.size()>0) {
					mjson.put("children", l);
				}
				list.add(mjson);
			}
			if(json.get("menus")!=null) {
				menustr = menustr.substring(1, menustr.length()-1);
				json.put("menus", menustr);
			}
		}
		json.put("menulist", list);
		result.setData(json);
		return result;
	}


	//树形子集查询
	private List<Object> getChildren(String parentId) {
		List<Object> list = new ArrayList<Object>();
		QueryWrapper<HbSysMenu> wrapper1 = new QueryWrapper<HbSysMenu>();
		wrapper1.eq("PARENT_ID", parentId);
		wrapper1.orderByAsc("MENU_ORDER");
		List<HbSysMenu> menulist = hbSysMenuMapper.selectList(wrapper1);
		if(menulist!=null && menulist.size()>0) {
			for(HbSysMenu m : menulist) {
				JSONObject mjson = new JSONObject();
				mjson.put("id", m.getId());
				mjson.put("label", m.getMenuName());
				List<Object> l = getChildren(m.getId());
				if(l!=null && l.size()>0) {
					mjson.put("children", l);
				}
				list.add(mjson);
			}
		}
		return list;
	}

	/**
		新增环保用户菜单关联表
	 */
	@Override
	public Result addHbUsersMenu(HbUsersMenu en,Result result) {
		en.setInsertDate(new Date());
		int i = hbUsersMenumapper.insert(en);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}

	/**
		更新环保用户菜单关联表
	 */
	@Override
	public Result updateHbUsersMenu(HbUsersMenu en,Result result) {
		HbUsersMenu usersMenu = hbUsersMenumapper.selectById(en.getUsersId());
		if(usersMenu==null) {
			en.setInsertDate(new Date());
			int i = hbUsersMenumapper.insert(en);
			if(i==1) {
				result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			}else {
				result.setSuccess(false);
				result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			}
			return result;
		}
		else {
			en.setModifyDate(new Date());
			int i = hbUsersMenumapper.updateById(en);
			if(i==1) {
				result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
			}else {
				result.setSuccess(false);
				result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			}
			return result;
		}
	}

	/**
		根据主键删除环保用户菜单关联表
	 */
	@Override
	public Result deleteHbUsersMenu(Serializable id,Result result) {
		int i = hbUsersMenumapper.deleteById(id);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}



}
